﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Moral_Teacher_DailyManage_Daily : ContainerBasePage
{
    /// <summary>
    /// 描述当前页面类型(0为当前学期数据  1为历史数据)
    /// </summary>
    private int pageType = 0;

    private static int[] MonthOfTerm1 = { 9, 10, 11, 12, 1, 2 };
    private static int[] MonthOfTerm2 = { 2, 3, 4, 5, 6, 7, 8 };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 获取页面类型 : 0为当前学期数据  1为历史数据
            if (!Int32.TryParse(Request["type"], out pageType))
                pageType = 0;

            InitDisplay();

            btnQuery_Click(btnQuery as object, new EventArgs());
        }
    }

    /// <summary>
    /// 初始化界面标签元素的显示行为
    /// </summary>
    protected void InitDisplay()
    {
        // 日期选择下拉框行为
        if (pageType == 0)
        {// 当前学期
            selYear.Style[HtmlTextWriterStyle.Display] = "none";
        }
        else
        {// 历史学期
            selYear.Style[HtmlTextWriterStyle.Display] = "block";
            BindSchoolYear();
        }

        // 年级选择下拉框行为
        BindGrade();
    }

    /// <summary>
    /// 绑定学期下拉框
    /// </summary>
    protected void BindSchoolYear()
    {
        //Hashtable ht = new Hashtable();
        //ht.Add("SchoolMark", SchoolMark);
        //IList<MoralityHistory> historyList = Container<IMoralityHistoryLogic>.Instance.FindAll(ht);
        //if (historyList == null || historyList.Count < 1)
        //{
        //    selYear.Items.Add(new ListItem("没有历史数据", "-1"));
        //}

        //foreach (MoralityHistory history in historyList)
        //{
        //    int year = 0;
        //    int term = 0;
        //    if (!Int32.TryParse(history.SchoolYear.ToString(), out year))
        //        return;
        //    if (!Int32.TryParse(history.Term.ToString(), out term))
        //        return;
        //    String str = Common.ConvertYearAndTerm(year, term);
        //    String value = String.Format("{0},{1}", history.SchoolYear, history.Term);
        //    selYear.Items.Add(new ListItem(str, value));
        //}

        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);
        IList<TermInfo> terms = Container<ITermInfoLogic>.Instance.FindAll(ht);
        foreach (TermInfo term in terms)
        {
            String str = Common.ConvertYearAndTerm(term.SchoolYear, term.Term);
            String value = String.Format("{0},{1}", term.SchoolYear, term.Term);
            selYear.Items.Add(new ListItem(str, value));
        }
    }

    /// <summary>
    /// 绑定年级下拉框
    /// </summary>
    protected void BindGrade()
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        IList<SchoolInfo> schoolList = Container<ISchoolInfoLogic>.Instance.FindAll(ht);
        switch (schoolList[0].SchoolOpt)
        {
            case 1:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                break;
            case 2:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 3:
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
            case 4:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 5:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
        }
    }

    /// <summary>
    /// 查询按钮事件
    /// </summary>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        int nYear = 0;
        int nTerm = 0;
        int nGrade = 0;

        // 获取页面类型 : 0为当前学期数据  1为历史数据
        if (!Int32.TryParse(Request["type"], out pageType))
            pageType = 0;

        if (pageType == 1)
        {// 当是历史界面时 获取学年学期数据
            string[] outString = selYear.SelectedValue.Split(new char[] { ',' });
            if (outString.Length != 2)
                //throw new Exception("orz The param is worng");
                return;

            if (!Int32.TryParse(outString[0], out nYear))
                //throw new Exception("orz The param is worng");
                return;

            if (!Int32.TryParse(outString[1], out nTerm))
                //throw new Exception("orz The param is worng");
                return;
        }
        if (!Int32.TryParse(selGrade.SelectedValue, out nGrade))
            //throw new Exception("orz The param is worng");
            return;

        // 查找年级内班级
        Hashtable ht = new Hashtable();
        IList<VClsInfo> clsList = null;
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("Grade", nGrade.ToString());
        if (pageType != 1)
        {
            ht.Add("IsEffect", "1");
            clsList = Container<IClsInfoLogic>.Instance.FindClassInfo(ht);
        }
        else 
        {
            ht.Add("SchoolYear", nYear);
            clsList = Container<IClsInfoLogic>.Instance.FindClassInfoEx(ht);
        }
        if(clsList == null || clsList.Count < 1)
            return;

        IList<int> idList = new List<int>();
        foreach (VClsInfo info in clsList)
        {
            idList.Add(info.RID);
        }
        IList<MoralityState> stateList = null;
        if (pageType == 1)
        {
            stateList =
                Container<IMoralityHistoryLogic>.Instance.GetState(nYear, nTerm,"", SchoolMark, idList);
            CreateTable(clsList, stateList, nYear, nTerm);
        }
        else
        {
             stateList =
                Container<IMoralityHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear, 
                                                                    SchoolCommon.Sch_SchoolInfo.SchoolTerm, "",
                                                                    SchoolMark, idList);
             CreateTable(clsList, stateList, 
                 SchoolCommon.Sch_SchoolInfo.SchoolYear, 
                 SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        }
        
    }

    /// <summary>
    /// 构造结果表格
    /// </summary>
    private void CreateTable(IList<VClsInfo> classInfo, IList<MoralityState> stateInfo, int year, int term)
    {
        int width = 90 + 70;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "班主任";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        hrow.Cells.Add(hcell);
        IList<DateTime> dateList = new List<DateTime>();
        ListSort<MoralityState> sort = new ListSort<MoralityState>(stateInfo, "StartTime", false);
        stateInfo = sort.Sort();


        int[] months;
        if (term == 1)
            months = MonthOfTerm1;
        else
            months = MonthOfTerm2;

        int index = 0;
        if (term == 1)
        {
            switch (DateTime.Now.Month)
            {
                case 9:
                    index = 0;
                    break;
                case 10:
                    index = -1;
                    break;
                case 11:
                    index = -2;
                    break;
                case 12:
                    index = -3;
                    break;
                case 1:
                    index = -4;
                    break;
                case 2:
                    index = -5;
                    break;
            }
        }
        else
        {
            switch (DateTime.Now.Month)
            {
                case 2:
                    index = 0;
                    break;
                case 3:
                    index = -1;
                    break;
                case 4:
                    index = -2;
                    break;
                case 5:
                    index = -3;
                    break;
                case 6:
                    index = -4;
                    break;
                case 7:
                    index = -5;
                    break;
                case 8:
                    index = -6;
                    break;
            }
        }

        foreach (int state in months)
        {

            hcell = new TableHeaderCell();
            hcell.Text = String.Format("{0}月", state);
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hrow.Cells.Add(hcell);
            if (term == 1)
            {
                if(state <=12)
                    dateList.Add(new DateTime(year, state, 1));
                else
                    dateList.Add(new DateTime(year + 1, state, 1));
            }
            else
            {
                dateList.Add(new DateTime(year + 1, state, 1));
            }
            
            if (state == DateTime.Now.Month)
                break;
            index++;
        }
        tbResult.Rows.Add(hrow);

        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);
        tbResult.CssClass = "useList1";
        // 填充表内容
        TableRow row = new TableRow();
        TableCell cell = new TableCell();
        foreach (VClsInfo info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsGradeName;
            row.Cells.Add(cell);
            cell = new TableCell();
            cell.Text = info.Header;
            row.Cells.Add(cell);

            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (MoralityState state in stateInfo)
                {
                    if (state.ClassID == info.RID && state.StartTime.Month == dt.Month)
                    {
                        // 构造超链html标签
                        HyperLink control = new HyperLink ();
                        control.Text = "已录入";
                        control.Attributes.Add("onclick", "detailView();");
                        control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}",
                                                year, term,
                                                state.ClassID, state.StartTime.Year, state.StartTime.Month);
                        cell.Controls.Add(control);
                        sign = true;
                        break;
                    }
                }
                if (!sign)
                    cell.Text = "未录入";

                row.Cells.Add(cell);
            }

            tbResult.Rows.Add(row);
        }
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "\\download\\" + String.Format("{0}_{1}_down.xls", Common.GetUserInfo().SchoolName, Common.GetUserInfo().UName);
        HttpContext.Current.Session["filepath"] = save_path;
        Meqs.Utilities.ExcelUtil.ExcelExport(save_path, "表格", 1, 0, cells);
        return "";
    }
}
